package controllers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import models.FeedBack;
import models.LeaMes;
import models.LendCard;
import models.MemType;
import models.Member;
import play.mvc.Controller;
import service.FeedBackService;
import service.LeaMesService;

public class FeedBackAction extends Controller {
	
	/**
	 * 会员添加反馈信息
	 * @param f	表单对象
	 */
	public static void addFeb(FeedBack f) { // /前台添加
		try {
			System.out.println("---------addFeb()---------");
			f.memId = 1001;
			f.febTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
					new Date(System.currentTimeMillis())).toString();
			FeedBackService.create(f);
			frontList(f.memId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询全部反馈信息
	 */
	public static void list() { // 全查
		List febList = FeedBackService.findAll();
		render(febList);
	}
	
	/**
	 * 查询个别用户的反馈信息
	 * @param memId	会员编号
	 */
	public static void frontList(long memId) {
		String userid = session.get("userid");
		Member meb=Member.findById(Long.parseLong(userid));
		if(meb == null){
			render("proscenium/login.html");
		}
		MemType memtype = MemType.findById(Long.parseLong(meb.metid));
		LendCard lendcard = LendCard.find("from LendCard where lecnum = ? ", meb.lecnum).first();
		memId = 1001;
		List febList = FeedBackService.findByPPL(memId); // 按会员的id来查询
		int i = febList.size();
		render("proscenium/feedback.html", febList, i,meb,memtype,lendcard);
	}
	
	/**
	 * 前台查看详情
	 * @param id	实际信息编号
	 * @param id2	会员反馈信息总条数
	 */
	public static void detail(long id, long id2) { // 前台查看
		String userid = session.get("userid");
		Member meb=Member.findById(Long.parseLong(userid));
		if(meb == null){
			render("proscenium/login.html");
		}
		MemType memtype = MemType.findById(Long.parseLong(meb.metid));
		LendCard lendcard = LendCard.find("from LendCard where lecnum = ? ", meb.lecnum).first();
		FeedBack feb = FeedBackService.findById(id);
		render(feb, id2,meb,memtype,lendcard);
	}

	/**
	 * 回复反馈信息
	 * @param repDet 回复内容
	 * @param id	反馈信息编号
	 */
	public static void saveFeb(String repDet, long id) { // 后台回复
		if (repDet == null ||"".equals(repDet.trim())) {
			flash.success("回复失败 ！");
		} else {
			FeedBack feb = FeedBackService.findById(id);
			feb.manId = 2012033101; // 拿到当前登陆进行留言的管理员id
			feb.repTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
					new Date(System.currentTimeMillis())).toString();
			feb.repDet = repDet;
			FeedBackService.save(feb);
			flash.success("回复成功 ！");
		}
		list();
	}
	
	/**
	 * 按编号查询反馈信息
	 * @param id 信息编号
	 */
	public static void show(long id) {
		FeedBack feb = FeedBackService.findById(id);
		String mes = null;
		if (feb.repDet == null || "".equals(feb.repDet)) { // 未回复的
			render("FeedBackAction/show.html", feb, mes);
		} else {
			mes = "已回复";
			render("FeedBackAction/show.html", feb, mes);
		}
	}
	
	/**
	 * 删除反馈信息
	 * @param id 信息编号
	 */
	public static void deleteFeb(long id) {
		try {
			FeedBackService.delete(id);
			flash.success("删除成功 ！");
		} catch (Exception e) {
			e.printStackTrace();
			flash.success("删除失败 ！");
		}
		list();
	}

}
